#include <fcntl.h>  
#include <unistd.h>  
#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
  
int main(int argc, char *argv[]) {  
    int fd = -1;
    char buffer[128];
    ssize_t read_len = 0;
    ssize_t write_len = 0;
    char *file_name = argv[1];
    char *write_data = argv[2];
  
    fd = open(file_name, O_WRONLY | O_CREAT, 0644);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }
    write_len = write(fd, write_data, strlen(write_data));
    if (write_len == -1) {
        perror("write");
        close(fd);
        exit(EXIT_FAILURE);
    }
    close(fd);
    int cnt = 0;
    while (cnt < 100)
    {
        cnt++;
        sleep(2);
        fd = open(file_name, O_RDONLY);
        if (fd == -1) {
            perror("open");
            exit(EXIT_FAILURE);
        }
        read_len = read(fd, buffer, sizeof(buffer) - 1);
        if (read_len == -1) {
            perror("read");
            close(fd);
            exit(EXIT_FAILURE);
        }
        buffer[read_len] = '\0';
        printf("Read from file: %s\n", buffer);
    }

    close(fd);
    return 0;  
}
